/**
 * 包间管理的modal
 * @author: minzhang
 * @update: 2017-03-17
 */

import {create, remove, update, detail, query,} from '../services/roomsManage';
import {message} from 'antd';
import {parse} from 'qs';

export default {

    namespace: 'roomsManage',

    state: {
        total: 0,
        pageNumber: 1,
        loading: false,
        pageSize: 10,
        list: [{
					key: '1',
					name: 'John Brown',
					type: '22',
					num: 3,
				}, {
					key: '2',
					name: 'Jim Green',
					type: '22',
					num: 4,
				}, {
					key: '3',
					name: '3',
					type: '22',
					num: 3,
				}],
        modalVisible: false,
        modalLoading: false,
        modalType: 'create',
        btnLoading: false,
				typeVisible: false,
				typeBtnLoading: false,
        item: {},
        query: {},
    },

    subscriptions: {
        setup({dispatch, history}) {
            history.listen(location => {
                if (location.pathname === '/rooms-manage') {
//                    dispatch({
//                        type: 'query',
//                        payload: {
//                            pageNumber: 1,
//                            pageSize: 10,
//                        },
//                    });
                }
            });
        }
    },

    effects: {
        *query({payload}, {call, put, select}) {
          yield put({type: 'showLoading'});
					const querys = yield select(state => state.techManage.query);
					const {data} = yield call(query, parse({...querys, ...payload}));
					if (data && data.success) {

						const {list, total, pageNumber, pageSize} = data.data.data;

						yield put({
								type: 'querySuccess',
								payload: {
										list,
										total,
										pageNumber,
										pageSize,
								},
						});
					} else {
							message.error(data && data.message || '数据获取失败');
					}
        },
        *'delete'({payload}, {call, put}) {
          yield put({type: 'showLoading'});
					const {pageNumber, pageSize} = payload;
					const {data} = yield call(remove, payload.id);
					if (data && data.success) {
							message.success('删除成功');
							yield put({
									type: 'query',
									payload: {
										pageNumber,
										pageSize,
									}
							});
					} else {
							message.error(data && data.message || '数据获取失败');
							yield put({type: 'hideLoading'});
					}
        },
        *create({payload}, {call, put}) {
          yield put({type: 'showBtnLoading'});
					const {formData, fn, pageNumber, pageSize} = payload;
					const {data} = yield call(create, formData);
					if (data && data.success) {
							fn()
							message.success('保存成功');
							yield put({type: 'hideModal'});
							yield put({type: 'hideBtnLoading'});
							yield put({
									type: 'query',
									payload: {
											pageNumber,
											pageSize,
									},
							});
					} else {
							yield put({type: 'hideBtnLoading'});
							message.error(data && data.message || '数据获取失败');
					}
        },
        *update({payload}, {select, call, put}) {
        	yield put({type: 'showBtnLoading'});
					const {formData, fn, pageNumber, pageSize} = payload;
					const {data} = yield call(update, formData);
					if (data && data.success) {
							fn();
							message.success('保存成功');
							yield put({type: 'hideModal'});
							yield put({type: 'hideBtnLoading'});
							yield put({
									type: 'query',
									payload: {
											pageNumber,
											pageSize,
									},
							});
					} else {
							message.error(data && data.message || '数据获取失败');
							yield put({type: 'hideBtnLoading'});
					}
        },
				*add({payload}, {select, call, put}) {
					yield put({
							type: 'showModal',
							payload: {
									modalType: 'create',
									modalLoading: true,
							}
					});
        },
        *detail({payload}, {select, call, put}) {
					yield put({
							type: 'showModal',
							payload: {
									modalType: payload.modalType,
									modalLoading: true
							}
					});
					const {id} = payload;
					const {data} = yield call(detail, id);
					if (data && data.success) {
							const res = data.data;

							yield put({
									type: 'updateAction',
									payload: {
											item: res,
									},
							});
					} else {
							message.error(data && data.message || '数据获取失败');
					}
        },
    },

    reducers: {
        showLoading(state) {
            return {...state, loading: true};
        },
        hideLoading(state) {
            return {...state, loading: false};
        },
        showBtnLoading(state) {
            return {...state, btnLoading: true};
        },
        hideBtnLoading(state) {
            return {...state, btnLoading: false};
        },
        querySuccess(state, action) {
            return {...state, ...action.payload, loading: false};
        },
        hideModal(state) {
            return {
                ...state,
                modalVisible: false,
                modalType: 'create',
                btnLoading: false,
                modalLoading: false,
                currentItem: {},
            };
        },
        showModal(state, action) {
            return {...state, ...action.payload, modalVisible: true};
        },
				showTypeModal(state, action) {
            return {...state, ...action.payload, typeVisible: true};
        },
				hideTypeModal(state) {
            return {...state, typeVisible: false, typeBtnLoading: false, };
        },
        updateAction(state, action) {
            return {...state, ...action.payload};
        },
    },
};
